package com.dotflat.satistics.service.impl;

import com.dotflat.satistics.dto.FinSatisticDTO;
import com.dotflat.satistics.dto.FinSatisticMetaDTO;
import com.dotflat.satistics.dto.FinSatisticProductDTO;
import com.dotflat.satistics.repository.crm.FinSatisticsRepository;
import com.dotflat.satistics.service.FinSatisticsService;
import com.matt.commons.page.PagerQuery;
import com.matt.foundation.vo.ClienteleListVO;
import com.matt.repository.ClienteleRepository;
import com.matt.repository.base.ClienteleNativeRepository;
import com.matt.service.ClienteleService;
import io.swagger.annotations.ApiModelProperty;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * *******************************
 * Created by Ron.
 * form com.matt
 * on 2018/8/2
 * *******************************
 */
@Service
public class FinSatisticsServiceImpl implements FinSatisticsService {

    @Autowired
    private ClienteleNativeRepository clienteleNativeRepository;

    @Autowired
    private FinSatisticsRepository satisticsRepository;

    @Override
    public Page<FinSatisticDTO> generalFinSatistics(String areaId, List<String> ids, String childName, String term,String beginDate,String endDate , PagerQuery pagerQuery){
//        //先根据条件查询clientele
//        Page<ClienteleListVO> list = clienteleNativeRepository.findByName(childName, areaId, null, pagerQuery);


//        String clienteleIds = list
        List<FinSatisticMetaDTO> metas = satisticsRepository.satisticsForInAndOut(areaId, ids,beginDate,endDate, pagerQuery);

        Map<String, FinSatisticDTO> cache = new HashMap<>();
        List<FinSatisticDTO> result = new ArrayList<>();
        for (FinSatisticMetaDTO meta : metas){
            FinSatisticDTO dto = cache.get(meta.getId());
            if (dto == null){
                dto = new FinSatisticDTO();
                dto.setItems(new ArrayList<>());
                dto.setClienteleId(meta.getId());
                dto.setClienteleName(meta.getName());
                dto.setChildName(meta.getChildName());
//                dto.setFullRecharge(0);
//                dto.setReturnAmount(0);
                cache.put(meta.getId(), dto);
                result.add(dto);
            }

            int productCount = 0;
            if (meta.getProductCount() != null){
                productCount = meta.getProductCount().intValue();
            }

            String productAmount = "0";
            if (meta.getProductAmount() != null){
                productAmount =  String.format("%.2f", meta.getProductAmount());
            }
            FinSatisticProductDTO productDTO = new FinSatisticProductDTO(meta.getProductName(),
                    meta.getProductId(), productCount, productAmount);
            dto.getItems().add(productDTO);

            BigDecimal nFullRecharge = new BigDecimal(dto.getFullRecharge());
            nFullRecharge = nFullRecharge.add(new BigDecimal(productAmount));
            dto.setFullRecharge( String.format("%.2f", nFullRecharge) );

            dto.setTotal(dto.getFullRecharge());
        }

        if (pagerQuery == null){
            return new PageImpl<>(result);
        }
        //然后查询对应的产品统计
        Long count = satisticsRepository.countSatisticsForInAndOut(areaId, ids);
        PageRequest pageRequest =  PageRequest.of(pagerQuery.getPageNum(), pagerQuery.getPageSize());
        return new PageImpl<>(result, pageRequest, count);
    }

}
